package com.cheyunkeji.er.activity.auction;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.app.PushMsgManager;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.PushMsgBean;
import com.cheyunkeji.er.fragment.auction.AuctionFragment;
import com.cheyunkeji.er.fragment.auction.GivePriceFragment;
import com.cheyunkeji.er.fragment.auction.MineFragment;
import com.cheyunkeji.er.fragment.auction.NotifyFragment;
import com.cheyunkeji.er.utils.ChannelInfoUtil;
import com.cheyunkeji.er.view.MyFragmentTabHost;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.fragment.app.Fragment;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;


public class HomeActivity extends BaseActivity {

    private static final String TAG = HomeActivity.class.getSimpleName();
    private MyFragmentTabHost myFrg;
    private LayoutInflater mInflater;
    private String[] title;
    private int[] imgIds = {R.drawable.er_bottom_notify_selector, R.drawable.er_bottom_auction_selector, R.drawable.er_bottom_price_selector, R.drawable.er_bottom_mine_selector};
    private Class[] fragSet = {NotifyFragment.class, AuctionFragment.class, GivePriceFragment.class, MineFragment.class};
    private String[] fragTags = {"notify", "auction", "price", "mine"};

    private Badge notifyBadge, mineBadge;

    @Override
    protected void initView() {

        mInflater = LayoutInflater.from(this);
        title = getResources().getStringArray(R.array.indicater_title);
        myFrg = (MyFragmentTabHost) findViewById(android.R.id.tabhost);
        myFrg.setup(HomeActivity.this, getSupportFragmentManager(), R.id.actual_content);
        notifyBadge = new QBadgeView(this);
        mineBadge = new QBadgeView(this);

        for (int i = 0; i < fragSet.length; i++) {

            View specView = mInflater.inflate(R.layout.er_indicator, null);
            ImageView ivHome = (ImageView) specView.findViewById(R.id.spec_iv);
            TextView tvHome = (TextView) specView.findViewById(R.id.spec_tv);
            RelativeLayout rlImage = (RelativeLayout) specView.findViewById(R.id.rl_iv);
            tvHome.setText(title[i]);
            ivHome.setBackgroundResource(imgIds[i]);

            TabHost.TabSpec spec1 = myFrg.newTabSpec(fragTags[i]);
            spec1.setIndicator(specView);

            if (i == 0) {
                notifyBadge.bindTarget(rlImage).setBadgeGravity(Gravity.TOP | Gravity.END).setShowShadow(false);
            }
            if (i == 3) {
                mineBadge.bindTarget(rlImage).setBadgeGravity(Gravity.TOP | Gravity.END).setShowShadow(false);
            }
            myFrg.addTab(spec1, fragSet[i], null);

        }

//      设置取消显示tab之间的分隔线
        myFrg.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
//      设置默认的选中项为第一个
        myFrg.setCurrentTab(0);

    }

    @Override
    protected void initData() {

        refreshBottomMsgCount();

    }


    @Override
    protected void init() {
        setContentView(R.layout.er_activity_home);
        EventBus.getDefault().register(this);
    }


    /**
     * 统一处理推送消息
     *
     * @param msgBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPushMsgReceived(PushMsgBean msgBean) {

        if (msgBean.getNotice_type() == PushMsgManager.MSG_TYPE_QUANMIAN) {

        } else if (msgBean.getNotice_type() == PushMsgManager.MSG_TYPE_ZHANNEIXIN) {

            refreshBottomMsgCount();
            if(myFrg.getCurrentTab() == 3){
                ((MineFragment) (getSupportFragmentManager().findFragmentByTag(fragTags[3]))).dealWithPushMsg();
            }
            EventBus.getDefault().removeStickyEvent(msgBean);
        } else {

            ((NotifyFragment) (getSupportFragmentManager().findFragmentByTag(fragTags[0]))).dealWithPushMsg(msgBean);
            EventBus.getDefault().removeStickyEvent(msgBean);
        }

    }


    /**
     * 切换Fragment
     *
     * @param currentFrag Activity当前显示的Fragment
     * @param newFrag     将要显示的Activity
     */
    public void changeFrag(Fragment currentFrag, Fragment newFrag) {
        if (!newFrag.isAdded()) {
            getSupportFragmentManager().beginTransaction().hide(currentFrag).add(R.id.actual_content, newFrag).commit();
        } else {
            getSupportFragmentManager().beginTransaction().hide(currentFrag).show(newFrag).commit();
        }
    }

    public Fragment getCurrentFragInTab() {
        return getSupportFragmentManager().findFragmentByTag(myFrg.getCurrentTabTag());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    /**
     * 未读消息数发生变化
     */
    public void refreshBottomMsgCount() {

        PushMsgManager pushMsgManager = PushMsgManager.getInstance(MyApplication.getInstance(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this).getAccount());

        int msgCountSum = pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_XUNJIA) + pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_YUZHAN) + pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_PAIMAI)
                + pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_ZHONGBIAO) + pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_LIUBIAO);
        notifyBadge.setBadgeNumber(msgCountSum);


        int msgCount = pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_ZHANNEIXIN);

        if (msgCount > 0) {
//          不显示消息记录条数，只显示圆点
            mineBadge.setBadgeNumber(-1);
        } else {
            mineBadge.setBadgeNumber(0);
        }
    }
}
